home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / YICN23.ZIP / UNITS / GADGETS.CPP < prev    next >
C/C++ Source or Header  |  1993-02-21  |  7KB  |  251 lines

  1.  
  2. #include "gadgets.h"
  3. #include "xlib.h"
  4. #include "xrect.h"
  5. #include "xline.h"
  6. #include "xpoint.h"
  7. #include "xtext.h"
  8.  
  9. #include <string.h>
  10. #include <conio.h>
  11. extern yakMouse mouse;
  12.  
  13. gadget::gadget(char icommandChar, char * filename, icon::flagType aflags, yakLib * myYakLib, gadget::flagType iflags, int ix, int iy):
  14.   animicon(filename, aflags, myYakLib)
  15. {
  16.   commandChar = icommandChar;
  17.   flags = iflags;
  18.   relX = ix;
  19.   relY = iy;
  20. }
  21.  
  22. gadget::gadget(char icommandChar, animiconNode * thisFrame, gadget::flagType iflags, int ix, int iy) :
  23.   animicon()
  24. {
  25.   commandChar = icommandChar;
  26.   flags = iflags;
  27.   relX = ix;
  28.   relY = iy;
  29.   animicon::add(thisFrame);
  30. }
  31.  
  32. byte gadget::isSelected(void) //0 if not; 1 if yes, 2 if selected for help.
  33. {
  34.   int mousex = mouse.x(), mousey = mouse.y(); //to reduce # of function calls;
  35.   if (mouse.isInBox(x, y, x+thisFrame->picture.width, y+thisFrame->picture.height))
  36.   {
  37.     if (((flags & touchPad) ? mouse.isPressed(yakMouse::leftButton) : mouse.isClicked(yakMouse::leftButton))
  38.        &&(x_get_pix(mousex, mousey, VisiblePageOffs) == thisFrame->picture.getPixel(mousex - x, mousey - y))
  39.        &&(x_get_pix(mousex, mousey, VisiblePageOffs) != 0))
  40.         return 1;
  41.     else if (mouse.isPressed(yakMouse::rightButton))
  42.         return 2;
  43.   }
  44.   else if (kbhit())
  45.   {
  46.     char tempchar = getch();
  47.     if (tempchar == commandChar)
  48.       return 1;
  49.     else
  50.     {
  51.       ungetch(tempchar);
  52.       return 0;
  53.     }
  54.   }
  55.   return 0;
  56. }
  57. //button functions follow------------------------------------->
  58.  
  59. button::button(int ix, int iy, char commandChar, int returnValue, char * filename, icon::flagType flags, yakLib * myYakLib, gadget::flagType iflags) :
  60.   gadget(commandChar, filename, flags, myYakLib, iflags, ix, iy),
  61.   returnValue(returnValue) {type = buttonType;}
  62.  
  63.  
  64. int button::status(void)
  65. {
  66.   byte isSel = isSelected();
  67.   return (isSel) ? ((isSel == 1) ? returnValue : (returnValue | HELP_MASK)) : 0;
  68. }
  69.  
  70. int button::activate(void)
  71. {
  72.   return 0;
  73. }
  74. //label functions follow------------------------------------->
  75. label::label(int ix, int iy, char commandChar, int returnValue, char * text, byte ifgColor, byte ibgColor, gadget::flagType iflags) :
  76.   button(ix, iy, commandChar, returnValue, "", icon::normal, NULL, iflags),
  77.   myText(text),
  78.   fgColor(ifgColor),
  79.   bgColor(ibgColor) {type = buttonType;}
  80.  
  81. void label::draw(int ix, int iy, word offset)
  82. {
  83.   x = ix;
  84.   y = iy;
  85.   x_bgprintf(x, y, offset, fgColor, bgColor, myText);
  86. }
  87.  
  88. byte label::isSelected(void)
  89. {
  90.   if (mouse.isInBox(x, y, x+strlen(myText)*CharWidth, y+CharHeight))
  91.   {
  92.     if ((flags & touchPad) ? mouse.isPressed(yakMouse::leftButton) : mouse.isClicked(yakMouse::leftButton))
  93.       return 1;
  94.     else if (mouse.isPressed(yakMouse::rightButton))
  95.       return 2;
  96.   }
  97.   else if (kbhit())
  98.   {
  99.     char tempchar = getch();
  100.     if (tempchar == commandChar)
  101.       return 1;
  102.     else
  103.     {
  104.       ungetch(tempchar);
  105.       return 0;
  106.     }
  107.   }
  108.   return 0;
  109. }
  110.  
  111.  
  112. //pSwitch functions follow----------------------------------->
  113.  
  114. pSwitch::pSwitch(int ix, int iy, char commandChar, char * filename, icon::flagType flags, yakLib * myYakLib, gadget::flagType iflags) :
  115.   gadget(commandChar, filename, flags, myYakLib, iflags, ix, iy),
  116.   position(0) {type = pSwitchType;}
  117.  
  118. int pSwitch::activate(void)
  119. {
  120.   position = (position == (numberOfFrames - 1)) ? 0 : (position + 1);
  121.   animicon::advance();
  122.   mouse.hide();
  123.   draw(x,y,VisiblePageOffs);
  124.   mouse.show();
  125.   return position;
  126. }
  127.  
  128. int pSwitch::status(void)
  129. {
  130.   if(isSelected())
  131.     activate();
  132.   return position;
  133. }
  134.  
  135. //slider functions follow-------------------------------------->
  136.  
  137.  
  138. slider::slider(int ix, int iy, int iwidth, int iheight, int iminimumValue, int imaximumValue, byte ibarColor, byte iborderColor, byte iindicatorColor) :
  139. gadget()
  140. {
  141.   relX = ix;
  142.   relY = iy;
  143.   width = iwidth;
  144.   height = iheight;
  145.   minimumValue = iminimumValue;
  146.   maximumValue = imaximumValue;
  147.   barColor = ibarColor;
  148.   borderColor = iborderColor;
  149.   position = (width / 2);
  150.   indicatorColor = iindicatorColor;
  151.   type = sliderType;
  152. }
  153.  
  154. void slider::draw(int ix, int iy, word offset)
  155. {
  156.   x = ix;
  157.   y = iy;
  158.   x_rect_fill(x, y, x+width, y+height, offset, borderColor); //yer basic bar
  159.   x_line(x,y, x+width, y, borderColor + 5, offset); //top
  160.   x_line(x,y, x, y+height-1, borderColor + 5, offset); //left
  161.   x_line(x,y+height-1, x+width-1, y+height-1, borderColor - 5, offset); //bottom
  162.   x_line(x+width,y, x+width, y+height-1, borderColor - 5, offset); //right
  163.   x_rect_fill(x+2, y+2, x+width-2, y+height-2, offset, barColor);
  164.   x_rect_fill(x + 2, y+2, x + position + 3, y+height -2, VisiblePageOffs, indicatorColor);
  165. }
  166.  
  167. void slider::draw(word offset)
  168. {
  169.   draw(x, y, offset);
  170. }
  171.  
  172. int slider::status(void)
  173. {
  174.   return(activate());
  175. }
  176.  
  177. int slider::activate(void)
  178. {
  179.   if ((mouse.isInBox(x, y, x+width, y+height))
  180.      &&(mouse.isPressed(yakMouse::leftButton)))
  181.   {
  182.     mouse.hide();
  183.     x_rect_fill(x+2, y+2, x+width-2, y+height-2, VisiblePageOffs, barColor);
  184.     x_rect_fill(x + 2, y+2, x + position + 3, y+height -2, VisiblePageOffs, indicatorColor);
  185.     position = mouse.x() - x - 1;
  186.     if (position < 0)
  187.       position = 0;
  188.     if (position > width - 5)
  189.       position = (width - 5);
  190.   mouse.show();
  191.   }
  192.   return (minimumValue + ((float)position/(float)(width - 5))*((float)(maximumValue - minimumValue)));
  193. }
  194.  
  195.  
  196.  
  197. //list functions follow---------------------------------------------->
  198. void gadgetList::draw(int ix, int iy, word offset)
  199. {
  200.   for (gadgetNode * thisGadget = firstGadget; thisGadget != NULL; thisGadget = thisGadget->nextGadget)
  201.     thisGadget->thisGadget->draw(ix + thisGadget->thisGadget->relX, iy + thisGadget->thisGadget->relY, offset);
  202. }
  203.  
  204. void gadgetList::draw(word offset)
  205. {
  206.   for (gadgetNode * thisGadget = firstGadget; thisGadget != NULL; thisGadget = thisGadget->nextGadget)
  207.     thisGadget->thisGadget->draw(offset);
  208. }
  209.  
  210. int gadgetList::status(void)
  211. {
  212.   int returnValue = 0;
  213.   int oldValue = 0;
  214.   for (gadgetNode * thisGadget = firstGadget; ((thisGadget != NULL) && (!returnValue)); thisGadget = thisGadget->nextGadget)
  215.   {
  216.     if (thisGadget->thisGadget->type == gadget::buttonType)
  217.       oldValue = thisGadget->thisGadget->status();
  218.     else
  219.       thisGadget->thisGadget->status();
  220.     if (oldValue)
  221.       returnValue = oldValue;
  222.   }
  223.   if (!(mouse.isPressed(yakMouse::eitherButton)))
  224.     mouse.isClicked(yakMouse::reset);
  225.  
  226.   return returnValue;
  227. }
  228.  
  229. void gadgetList::add(gadgetNode * theNewGadget)
  230. {
  231.   if (firstGadget != NULL)
  232.   {
  233.     lastGadget->nextGadget = theNewGadget; // make the new node
  234.     lastGadget->nextGadget->prevGadget = lastGadget; //make new node point back
  235.     firstGadget->prevGadget = lastGadget;
  236.     lastGadget = lastGadget->nextGadget;
  237.   }
  238.   else
  239.   {
  240.     firstGadget = theNewGadget; // make the new node
  241.     lastGadget = firstGadget;
  242.     lastGadget->prevGadget = firstGadget; //make new node point back
  243.     firstGadget->prevGadget = lastGadget; //so it wraps backwards too.
  244.   }
  245.   if (lastGadget->nextGadget)
  246.     add(lastGadget->nextGadget);
  247.   else
  248.     lastGadget->nextGadget = NULL;
  249. }
  250.  
  251.